/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministrarModulosInternalFrame.java
 *
 * Created on 18/06/2013, 06:28:48 PM
 */
package pe.edu.unmsm.cinfo.siga.ui.ventanas;

import pe.edu.unmsm.cinfo.siga.ui.utils.EstadoRenderer;
import java.awt.Component;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collections;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import org.jdesktop.application.Action;
import pe.edu.unmsm.cinfo.siga.datos.Modulo;
import pe.edu.unmsm.cinfo.siga.datos.ModuloCurso;
import pe.edu.unmsm.cinfo.siga.datos.ModuloCursoPK;
import pe.edu.unmsm.cinfo.siga.ui.Main;

/**
 *
 * @author lartkma
 */
public class AdministrarModulosInternalFrame extends javax.swing.JInternalFrame {
    private class RepitenciaRenderer extends DefaultTableCellRenderer{
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if(value != null && value.equals((short)1)){
                setText("Si");
            }else{
                setText("No");
            }
            return this;
        }
        
    }
    
    /** Creates new form AdministrarModulosInternalFrame */
    public AdministrarModulosInternalFrame() {
        initComponents();
        establecerOyentes();
    }
    
    private void establecerOyentes(){
        modulosTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                getChangeSupport().firePropertyChange("seleccionado", !isSeleccionado(), isSeleccionado());
            }
        });
    }

    public PropertyChangeSupport getChangeSupport() {
        if(changeSupport == null) changeSupport = new PropertyChangeSupport(this);
        return changeSupport;
    }

    public Modulo getModuloSeleccionado() {
        return moduloSeleccionado;
    }

    public void setModuloSeleccionado(Modulo moduloSeleccionado) {
        Modulo old = this.moduloSeleccionado;
        this.moduloSeleccionado = moduloSeleccionado;
        getChangeSupport().firePropertyChange("moduloSeleccionado", old, moduloSeleccionado);
    }
    
    @Override
    public void addPropertyChangeListener(PropertyChangeListener l){
        getChangeSupport().addPropertyChangeListener(l);
    }
    
    @Override
    public void removePropertyChangeListener(PropertyChangeListener l){
        getChangeSupport().removePropertyChangeListener(l);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AplicacionSIGAPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getResourceMap(AdministrarModulosInternalFrame.class);
        consultaModulos = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("consultaModulos.query")); // NOI18N
        listaModulos = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(consultaModulos.getResultList());
        moduloSeleccionado = new pe.edu.unmsm.cinfo.siga.datos.Modulo();
        anteriorButton = new javax.swing.JButton();
        siguienteButton = new javax.swing.JButton();
        listadoDetalleTabPanel = new javax.swing.JTabbedPane();
        panelListado = new javax.swing.JScrollPane();
        modulosTable = new javax.swing.JTable();
        panelPropiedades = new javax.swing.JPanel();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        codigoTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        duracionTextField = new javax.swing.JTextField();
        descripcionTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        estadoTextField = new javax.swing.JTextField();
        repitenciaTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        minimoAlumnosTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
        maximoCuotasTextField = new javax.swing.JTextField();
        javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        cursosTable = new javax.swing.JTable();
        actualizarButton = new javax.swing.JButton();
        ultimoButton = new javax.swing.JButton();
        primerButton = new javax.swing.JButton();
        modificarButton = new javax.swing.JButton();
        nuevoButton = new javax.swing.JButton();
        eliminarButton = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(966, 515));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getActionMap(AdministrarModulosInternalFrame.class, this);
        anteriorButton.setAction(actionMap.get("anteriorAction")); // NOI18N
        anteriorButton.setIcon(resourceMap.getIcon("anteriorButton.icon")); // NOI18N
        anteriorButton.setText(resourceMap.getString("anteriorButton.text")); // NOI18N
        anteriorButton.setToolTipText(resourceMap.getString("anteriorButton.toolTipText")); // NOI18N
        anteriorButton.setName("anteriorButton"); // NOI18N

        siguienteButton.setAction(actionMap.get("siguienteAction")); // NOI18N
        siguienteButton.setIcon(resourceMap.getIcon("siguienteButton.icon")); // NOI18N
        siguienteButton.setText(resourceMap.getString("siguienteButton.text")); // NOI18N
        siguienteButton.setToolTipText(resourceMap.getString("siguienteButton.toolTipText")); // NOI18N
        siguienteButton.setName("siguienteButton"); // NOI18N

        listadoDetalleTabPanel.setName("listadoDetalleTabPanel"); // NOI18N

        panelListado.setName("panelListado"); // NOI18N

        modulosTable.setDragEnabled(true);
        modulosTable.setName("modulosTable"); // NOI18N
        modulosTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaModulos, modulosTable, "modulosTableBinding"); // NOI18N
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modCod}"));
        columnBinding.setColumnName("Mod Cod");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modNom}"));
        columnBinding.setColumnName("Mod Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modDesc}"));
        columnBinding.setColumnName("Mod Desc");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modDura}"));
        columnBinding.setColumnName("Mod Dura");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modNmin}"));
        columnBinding.setColumnName("Mod Nmin");
        columnBinding.setColumnClass(Short.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modEst}"));
        columnBinding.setColumnName("Mod Est");
        columnBinding.setColumnClass(Short.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modRec}"));
        columnBinding.setColumnName("Mod Rec");
        columnBinding.setColumnClass(Short.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado}"), modulosTable, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
        bindingGroup.addBinding(binding);

        modulosTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                irADetalle(evt);
            }
        });
        panelListado.setViewportView(modulosTable);
        modulosTable.getColumnModel().getColumn(0).setMinWidth(50);
        modulosTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title0")); // NOI18N
        modulosTable.getColumnModel().getColumn(1).setMinWidth(67);
        modulosTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title1")); // NOI18N
        modulosTable.getColumnModel().getColumn(2).setMinWidth(500);
        modulosTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title2")); // NOI18N
        modulosTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title3")); // NOI18N
        modulosTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title4")); // NOI18N
        modulosTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title5")); // NOI18N
        modulosTable.getColumnModel().getColumn(5).setCellRenderer(new EstadoRenderer());
        modulosTable.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("modulosTable.columnModel.title6")); // NOI18N
        modulosTable.getColumnModel().getColumn(6).setCellRenderer(new RepitenciaRenderer());

        listadoDetalleTabPanel.addTab("Listado", panelListado);

        panelPropiedades.setName("panelPropiedades"); // NOI18N

        jLabel1.setText("Código"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        codigoTextField.setEditable(false);
        codigoTextField.setName("codigoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modCod}"), codigoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("Duración"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        duracionTextField.setEditable(false);
        duracionTextField.setName("duracionTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modDura}"), duracionTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        descripcionTextField.setEditable(false);
        descripcionTextField.setName("descripcionTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modDesc}"), descripcionTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "descripcionTextFieldBind"); // NOI18N
        bindingGroup.addBinding(binding);

        jLabel3.setText("Descripción"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Estado"); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        estadoTextField.setEditable(false);
        estadoTextField.setName("estadoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modEst}"), estadoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        repitenciaTextField.setEditable(false);
        repitenciaTextField.setName("repitenciaTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modRec}"), repitenciaTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel5.setText("Repitencia"); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Mín. alumnos"); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        minimoAlumnosTextField.setEditable(false);
        minimoAlumnosTextField.setName("minimoAlumnosTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modNmin}"), minimoAlumnosTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel7.setText("Max. cuotas"); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        maximoCuotasTextField.setEditable(false);
        maximoCuotasTextField.setName("maximoCuotasTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modMaxcuota}"), maximoCuotasTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel8.setText("Cursos correspondientes al módulo"); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        cursosTable.setColumnSelectionAllowed(true);
        cursosTable.setName("cursosTable"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.moduloCursoList}");
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, cursosTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modcOrd}"));
        columnBinding.setColumnName("Modc Ord");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cursoBase.curbNom}"));
        columnBinding.setColumnName("Curso Base.curb Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modcNhor}"));
        columnBinding.setColumnName("Modc Nhor");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        jTableBinding.setSourceNullValue(Collections.EMPTY_LIST);
        jTableBinding.setSourceUnreadableValue(Collections.EMPTY_LIST);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(cursosTable);
        cursosTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        cursosTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("cursosTable.columnModel.title0")); // NOI18N
        cursosTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("cursosTable.columnModel.title1")); // NOI18N
        cursosTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("cursosTable.columnModel.title2")); // NOI18N

        javax.swing.GroupLayout panelPropiedadesLayout = new javax.swing.GroupLayout(panelPropiedades);
        panelPropiedades.setLayout(panelPropiedadesLayout);
        panelPropiedadesLayout.setHorizontalGroup(
            panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPropiedadesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 905, Short.MAX_VALUE)
                    .addGroup(panelPropiedadesLayout.createSequentialGroup()
                        .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(duracionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(panelPropiedadesLayout.createSequentialGroup()
                                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(estadoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(repitenciaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addGap(18, 18, 18)
                                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(minimoAlumnosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(maximoCuotasTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7)))
                            .addComponent(descripcionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel8))
                .addContainerGap())
        );
        panelPropiedadesLayout.setVerticalGroup(
            panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPropiedadesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(descripcionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPropiedadesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(duracionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(estadoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(repitenciaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(minimoAlumnosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(maximoCuotasTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addContainerGap())
        );

        listadoDetalleTabPanel.addTab("Detalle", panelPropiedades);

        actualizarButton.setAction(actionMap.get("actualizarAction")); // NOI18N
        actualizarButton.setIcon(resourceMap.getIcon("actualizarButton.icon")); // NOI18N
        actualizarButton.setName("actualizarButton"); // NOI18N

        ultimoButton.setAction(actionMap.get("ultimoAction")); // NOI18N
        ultimoButton.setIcon(resourceMap.getIcon("ultimoButton.icon")); // NOI18N
        ultimoButton.setText(resourceMap.getString("ultimoButton.text")); // NOI18N
        ultimoButton.setToolTipText(resourceMap.getString("ultimoButton.toolTipText")); // NOI18N
        ultimoButton.setName("ultimoButton"); // NOI18N

        primerButton.setAction(actionMap.get("primeroAction")); // NOI18N
        primerButton.setIcon(resourceMap.getIcon("primerButton.icon")); // NOI18N
        primerButton.setText(resourceMap.getString("primerButton.text")); // NOI18N
        primerButton.setToolTipText(resourceMap.getString("primerButton.toolTipText")); // NOI18N
        primerButton.setName("primerButton"); // NOI18N

        modificarButton.setAction(actionMap.get("modificarModuloSeleccionado")); // NOI18N
        modificarButton.setName("modificarButton"); // NOI18N

        nuevoButton.setAction(actionMap.get("nuevoModulo")); // NOI18N
        nuevoButton.setText(resourceMap.getString("nuevoButton.text")); // NOI18N
        nuevoButton.setName("nuevoButton"); // NOI18N

        eliminarButton.setAction(actionMap.get("eliminarModulo")); // NOI18N
        eliminarButton.setName("eliminarButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listadoDetalleTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 930, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nuevoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modificarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 416, Short.MAX_VALUE)
                        .addComponent(primerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(anteriorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(siguienteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ultimoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(actualizarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(listadoDetalleTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(nuevoButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(modificarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(eliminarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(actualizarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ultimoButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(siguienteButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(anteriorButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(primerButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void irADetalle(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_irADetalle
    if (evt.getClickCount() > 1) {
        //int indice = listadoDetalleTabPanel.indexOfTabComponent(panelPropiedades);
        listadoDetalleTabPanel.setSelectedIndex(1);
    }
}//GEN-LAST:event_irADetalle

    @Action(enabledProperty="seleccionado")
    public void modificarModuloSeleccionado(){
        int indice = modulosTable.convertRowIndexToModel(modulosTable.getSelectedRow());
        
        JFrame mainFrame = Main.getApplication().getMainFrame();
        NuevoModuloDialog dialogo = new NuevoModuloDialog(mainFrame, true);
        dialogo.setLocationRelativeTo(mainFrame);
        dialogo.setModuloEditado(listaModulos.get(indice));
        
        modulosTable.clearSelection();
        setModuloSeleccionado(null);
        codigoTextField.setText("");
        descripcionTextField.setText("");
        duracionTextField.setText("");
        estadoTextField.setText("");
        repitenciaTextField.setText("");
        minimoAlumnosTextField.setText("");
        maximoCuotasTextField.setText("");
        
        dialogo.setVisible(true);
        if(dialogo.isGuardado()){
            try{
                entityManager.getTransaction().begin();
                Modulo aGuardar = dialogo.getModuloEditado();
                for(ModuloCurso mc : aGuardar.getModuloCursoList()){
                    mc.getModuloCursoPK().setModCod(aGuardar.getModCod());
                }
                entityManager.merge(aGuardar);
                entityManager.getTransaction().commit();
                listaModulos.set(indice, dialogo.getModuloEditado());
            }catch (Exception e){
                entityManager.getTransaction().rollback();
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
        }else{
            entityManager.refresh(listaModulos.get(indice));
        }
        modulosTable.setRowSelectionInterval(indice, indice);
    }

    @Action
    public void nuevoModulo() {
        JFrame mainFrame = Main.getApplication().getMainFrame();
        NuevoModuloDialog dialogo = new NuevoModuloDialog(mainFrame, true);
        dialogo.setLocationRelativeTo(mainFrame);
        dialogo.setModuloEditado(new Modulo());
        dialogo.setVisible(true);
        if(dialogo.isGuardado()){
            try{
                entityManager.getTransaction().begin();
                guardarModulo(dialogo.getModuloEditado());
                entityManager.getTransaction().commit();
                listaModulos.add(dialogo.getModuloEditado());
                ultimoAction();
            }catch(Exception e){
                entityManager.getTransaction().rollback();
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
        }
    }
    
    private void guardarModulo(Modulo aGuardar){
        List<ModuloCurso> cursos = aGuardar.getModuloCursoList();
        aGuardar.setModuloCursoList(Collections.EMPTY_LIST);
        entityManager.persist(aGuardar);
        if(cursos != null) for(ModuloCurso mc : cursos){
            mc.getModuloCursoPK().setModCod(aGuardar.getModCod());
        }
        aGuardar.setModuloCursoList(cursos);
        entityManager.merge(aGuardar);
    }

    @Action(enabledProperty="seleccionado")
    public void eliminarModulo() {
        int indice = modulosTable.convertRowIndexToModel(modulosTable.getSelectedRow());
        if(seConfirma("¿Seguro que desea eliminar " +
                listaModulos.get(indice).getModNom() + "?")){
            Modulo aEliminar = listaModulos.get(indice);
            aEliminar.setModEst((short)2);
            entityManager.merge(aEliminar);
            entityManager.getTransaction().begin();
            entityManager.getTransaction().commit();
            listaModulos.remove(aEliminar);
            modulosTable.clearSelection();
        }
    }
    
    private boolean seConfirma(String mensaje){
        int n = JOptionPane.showConfirmDialog(null, mensaje,
                "Aviso", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null);
        return n == JOptionPane.YES_OPTION;
    }

    public boolean isSeleccionado() {
        return modulosTable.getSelectedRow() != -1;
    }

    @Action
    public void primeroAction() {
        mostrarFila(0);
    }

    @Action
    public void anteriorAction() {
        int previo = modulosTable.getSelectedRow() - 1;
        if (previo < 0) previo = 0;
        mostrarFila(previo);
    }

    @Action
    public void siguienteAction() {
        int siguiente = modulosTable.getSelectedRow() + 1;
        if(siguiente > modulosTable.getRowCount()-1) siguiente = modulosTable.getRowCount()-1;
        mostrarFila(siguiente);
    }

    @Action
    public void ultimoAction() {
        mostrarFila(modulosTable.getRowCount()-1);
    }

    @Action
    public void actualizarAction() {
        return;
    }
    
    private void mostrarFila(int fila){
        modulosTable.setRowSelectionInterval(fila, fila);
        modulosTable.scrollRectToVisible(modulosTable.getCellRect(fila, 0, true));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton actualizarButton;
    private javax.swing.JButton anteriorButton;
    private javax.swing.JTextField codigoTextField;
    private javax.persistence.Query consultaModulos;
    private javax.swing.JTable cursosTable;
    private javax.swing.JTextField descripcionTextField;
    private javax.swing.JTextField duracionTextField;
    private javax.swing.JButton eliminarButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField estadoTextField;
    private java.util.List<Modulo> listaModulos;
    private javax.swing.JTabbedPane listadoDetalleTabPanel;
    private javax.swing.JTextField maximoCuotasTextField;
    private javax.swing.JTextField minimoAlumnosTextField;
    private javax.swing.JButton modificarButton;
    private pe.edu.unmsm.cinfo.siga.datos.Modulo moduloSeleccionado;
    private javax.swing.JTable modulosTable;
    private javax.swing.JButton nuevoButton;
    private javax.swing.JScrollPane panelListado;
    private javax.swing.JPanel panelPropiedades;
    private javax.swing.JButton primerButton;
    private javax.swing.JTextField repitenciaTextField;
    private javax.swing.JButton siguienteButton;
    private javax.swing.JButton ultimoButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private PropertyChangeSupport changeSupport;
}
